package com.pencilisland.pay.app.service.impl;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.mp.core.MessageRequest;
import com.pencilisland.pay.api.config.AppConfig;
import com.pencilisland.pay.app.service.RestService;

@Component
public class RestServiceImpl implements RestService {
	@Autowired
	AppConfig appConfig;
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	ObjectMapper objectMapper;

	@Override
	public <T> T sendToCoreMP(String messageId, Object body, Class<T> returnType) {
		MessageRequest request = new MessageRequest();
		request.setMessageId(messageId);
		request.setBody(body);

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

		String url = appConfig.getCoreServer() + "/core/mp";

		HttpEntity<MessageRequest> httpRequest = new HttpEntity<>(request, headers);

		return restTemplate.postForObject(url, httpRequest, returnType);
	}

}
